﻿Imports System.Windows.Forms

Public Class cConsultas
    Private lqSelect As String
    Private lqGroup As String
    Private lqWhere As String
    Private lCuentaW As Int16 = 0
    Public Sub New(ByVal Contenedor As Control)
        lCuentaW = 0
        lqSelect = "SELECT "
        lqGroup = "GROUP BY "
        lqWhere = "WHERE "
        GeneraSelectGroup(Contenedor)
    End Sub
    Private Function EsAgregado(ByVal Cadena As String) As Boolean
        Dim TemEs As Boolean = False
        For Each Agre As String In "SUM,MAX,MIN,COUNT,AVG".Split(",")
            If Cadena.ToUpper.IndexOf(Agre) <> -1 Then
                TemEs = True
                Exit For
            End If
        Next
        Return TemEs
    End Function
    Private Sub GeneraSelectGroup(ByVal Contenedor As Control)
        Dim TemSel As String = ""
        Dim TemGr As String = ""
        For i As Int16 = Contenedor.Controls.Count - 1 To 0 Step -1
            Dim ctl As Control = Contenedor.Controls(i)
            If CType(ctl, CheckBox).Checked Then
                TemSel &= ctl.Tag.ToString & ", "
                If Not EsAgregado(ctl.Tag.ToString) Then _
                    TemGr &= ctl.Tag.ToString & ", "
            End If
            ctl = Nothing
        Next
        lqSelect &= TemSel.Substring(0, TemSel.Length - 2)
        lqGroup &= TemGr.Substring(0, TemGr.Length - 2)
    End Sub
    Public Function AddWhere(ByVal Campo As String, ByVal DesdeVal As String, ByVal HastaVal As String, ByVal SignoCompara As String) As Boolean
        Dim EsAdd As Boolean = False
        If DesdeVal.Trim.Length > 0 And HastaVal.Trim.Length > 0 Then
            lqWhere &= Campo & " " & SignoCompara & " :P" & Campo & "Desde AND :P" & Campo & "Hasta "
            If lCuentaW > 0 Then _
                lqWhere &= " AND "
            lCuentaW += 1
            EsAdd = True
        ElseIf DesdeVal.Trim.Length > 0 Then
            lqWhere &= Campo & " " & SignoCompara & " :P" & Campo
            If lCuentaW > 0 Then _
                lqWhere &= " AND "
            lCuentaW += 1
            EsAdd = True
        End If
        Return EsAdd
    End Function
    Public ReadOnly Property qSelect() As String
        Get
            Return lqSelect
        End Get
    End Property
    Public ReadOnly Property qGroup() As String
        Get
            Return lqGroup
        End Get
    End Property
    Public ReadOnly Property qWhere() As String
        Get
            If lCuentaW = 0 Then
                Return ""
            Else
                Return lqWhere
            End If
        End Get
    End Property
    Private Function GetAnchoTexto(ByVal col As DataColumn, ByVal dt As DataTable) As Integer
        If dt.Rows.Count > 0 Then
            Dim dr As DataRow = dt.Rows(0)
            Return dr(col.ColumnName).ToString.Length
        End If
    End Function
    Public Sub ConfigDataGrid(ByVal dg As DataGridView, ByVal dt As DataTable, Optional ByVal dgSoloLectura As Boolean = False, Optional ByVal DefinirAnchoTexto As Boolean = False)
        dg.Columns.Clear()
        dg.AutoGenerateColumns = False
        For Each col As DataColumn In dt.Columns
            Dim dgCol As New DataGridViewColumn(New DataGridViewTextBoxCell)
            dgCol.Name = col.ColumnName
            dgCol.DataPropertyName = col.ColumnName
            If col.Caption.Trim.Length > 0 Then
                dgCol.HeaderText = col.Caption
            Else
                dgCol.HeaderText = col.ColumnName
            End If
            dgCol.ReadOnly = dgSoloLectura
            If col.DataType Is System.Type.GetType("System.Decimal") Or col.DataType Is System.Type.GetType("System.Double") Then
                dgCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                dgCol.DefaultCellStyle.Format = "c2"
                dgCol.Width = 120
            ElseIf col.DataType Is System.Type.GetType("System.Int16") Or col.DataType Is System.Type.GetType("System.Int32") Or col.DataType Is System.Type.GetType("System.Int64") Or col.DataType Is System.Type.GetType("System.Integer") Then
                dgCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                dgCol.Width = 70
            ElseIf col.DataType Is System.Type.GetType("System.DateTime") Then
                dgCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dgCol.DefaultCellStyle.Format = "d"
                dgCol.Width = 80
            Else
                If DefinirAnchoTexto Then
                    dgCol.Width = GetAnchoTexto(col, dt) * 8
                Else
                    dgCol.Width = col.MaxLength * 8
                End If
            End If
            dg.Columns.Add(dgCol)
        Next
        dg.DataSource = dt
    End Sub

End Class
